<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="请选择房源类型" prop="rentInstallmentFlg">
        <el-select v-model="queryParams.rentInstallmentFlg" placeholder="请选择房源类型" @change="changeFlg" clearable>
          <el-option
            v-for="item in flgOpt"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="租客姓名" prop="tenantName">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入租客姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租客手机号" prop="tenantPhone">
        <el-input
          v-model="queryParams.tenantPhone"
          placeholder="请输入租客手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请输入小区名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入租客手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请选择起租日期" prop="starttime">
        <el-date-picker clearable
                        v-model="queryParams.starttime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择起租日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="tenantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租客姓名" align="center" prop="tenantName" />
      <el-table-column label="租客合同编号" align="center" prop="cfContractNo" />
      <el-table-column label="租客手机号" align="center" prop="tenantPhone" />
      <el-table-column label="房源信息" align="center" prop="roomName" />
      <el-table-column label="房源类型" align="center" prop="rentInstallmentFlg" />
      <el-table-column label="起租日期" align="center" prop="starttime" />
      <el-table-column label="结束日期" align="center" prop="endtime" />
      <el-table-column label="押付方式" align="center" prop="paymentType" />
      <el-table-column label="押金" align="center" prop="monthRent" />
      <el-table-column label="租金" align="center" prop="deposit" />
      <el-table-column label="状态" align="center" prop="freezeStatus" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listTenant, getTenant, delTenant, addTenant, updateTenant } from "@/api/termination/tenants";

export default {
  name: "Tenant",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      tenantList: [],
      flgOpt:[{
        value: '0',
        label: '整租(分散式)'
      },
        {
          value: '1',
          label: '合租(分散式)'
        },
        {
          value: '2',
          label: '公寓(集中式)'
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        tenantName: null,
        tenantCertificateType: null,
        tenantCertificateNo: null,
        tenantPhone: null,
        tenantRelationAddress: null,
        emergencyRelation: null,
        starttime: null,
        roomName: null,
        rentInstallmentFlg: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listTenant(this.queryParams).then(response => {
        this.tenantList = response.rows;
        response.rows.forEach(i=>{
          if(i.rentInstallmentFlg=0){
            i.rentInstallmentFlg='整租(分散式)'
          }
          if(i.rentInstallmentFlg=1){
            i.rentInstallmentFlg='合租(分散式)'
          }
          if(i.rentInstallmentFlg=2){
            i.rentInstallmentFlg='公寓(集中式)'
          }
          if(i.paymentType=1){
            i.paymentType='押1付2'
          }
          if(i.freezeStatus=0){
            i.freezeStatus='未结束'
          }
          if(i.freezeStatus=1){
            i.freezeStatus='已结束'
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        tenantName: null,
        tenantCertificateType: null,
        tenantCertificateNo: null,
        tenantPhone: null,
        tenantRelationAddress: null,
        emergencyRelation: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTenant(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTenant(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTenant(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTenant(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tenant/export', {
        ...this.queryParams
      }, `system_tenant.xlsx`)
    }
  }
};
</script>
